from pygtrans import Translate


# 选用谷歌翻译将 old_language 语种，翻译为 new_language 目标语种
def google_translate_texts(old_data_list, old_language, new_language, english_language):
    """
    :param old_data_list: list[]，需要翻译的数据列表
    :param old_language: 需要翻译的源语言
    :param new_language: 翻译后的目标语言
    :param english_language: 原英语，用来做首字母有没有大写的判断，如果有大写，其他多语言首字母也大写
    :return: list 返回翻译后的目标语言数据列表
    """
    # 创建谷歌翻译对象
    client = Translate()
    # 将源数据按照
    new_datas = client.translate(old_data_list, source=old_language, fmt='text', target=new_language)
    new_data_list = []
    for i in new_datas:
        new_data_list.append(i.translatedText)
    capitalize_new_data_list = []

    # 根据 english 源语言是否进行了首字母大写，判断是否要对其他语种的首字母进行大写
    for a, b in zip(english_language, new_data_list):
        # 判断字符串长度为非 0
        if len(a) > 0:
            # 首字母大写则进行大写替换
            if a[0].isupper() is True:
                b = b.capitalize()
            # 首字母非大写则进行小写替换
            else:
                b = b[:1].lower() + b[1:]
        capitalize_new_data_list.append(b)
    return capitalize_new_data_list


noun_list = ['Pica AI', 'Artguru']
noun_dict = {'Pica AI': '{{1}}', 'Artguru': '{{2}}'}


# 谷歌翻译进行名词保护，即在翻译前将需要保护的名词加上 {{}}，例如 Pica AI，替换成{{Pica AI}}
def input_noun_protection(pd_result_data):
    """
    :param pd_result_data: 输入原始的数据 dict 类型
    :return:输出替换后的数据 dict 类型，结构不动
    """
    en_list = pd_result_data['en_英语']
    cn_list = pd_result_data['zh-CN_中文']
    new_en_list = []
    new_cn_list = []

    # 英语来一遍
    for i in en_list:
        new_i = ''
        for j in noun_list:
            new_i = i.replace(f'{j}', f'{noun_dict[j]}')
            i = new_i
        new_en_list.append(new_i)

    # 中文来一遍
    for n in cn_list:
        new_n = ''
        for k in noun_list:
            new_n = n.replace(f'{k}', f'{noun_dict[k]}')
            n = new_n
        new_cn_list.append(new_n)
    new_pd_result_data = {'en_英语': new_en_list, 'zh-CN_中文': new_cn_list}
    return new_pd_result_data


# 名词保护，将翻译过后的数据进行名词保护的还原，替换好的 {{Pica AI}} 还原成 Pica AI
def output_noun_protection(old_data_list):
    new_data_list = []
    for i in old_data_list:
        new_i = ''
        for j in noun_list:
            new_i = i.replace(f'{noun_dict[j]}', f'{j}')
            i = new_i
        new_data_list.append(new_i)
    return new_data_list


# 将翻译结果中的英文' 替换成 \‘
def english_point_replace(old_data_list):
    new_data_list = []
    for i in old_data_list:
        new_i = i.replace("'", "\\'")
        new_data_list.append(new_i)
    return new_data_list


# 给 web和 ios app 用的把占位符替换成 web 前端和ios app 需要的占位符 {{a}}
def placeholder_replace(pd_result_data, client):
    en_list = pd_result_data['en_英语']
    cn_list = pd_result_data['zh-CN_中文']
    new_en_list = []
    new_cn_list = []

    # 产品可能提供的占位符号
    if client == 'web':
        placeholder_replace_list = ["%Y-%m-%d", "%d", "XXXX"]
        placeholder_replace_dict = {"%Y-%m-%d": "{{z}}", "%d": "{{x}}", "XXXX": "{{x}}"}
        random_letter = ["{{a}}", "{{b}}", "{{c}}", "{{d}}", "{{e}}", "{{f}}"]
    elif client == 'ios':
        placeholder_replace_list = ["%Y-%m-%d", "XXXX", '{{x}}']
        placeholder_replace_dict = {"%Y-%m-%d": "%@", "XXXX": "%d", '{{x}}': "%d"}
        random_letter = ["%1$d", "%2$d", "%3$d", "%4$d", "%5$d", "%6$d"]
    else:
        raise print("没有在 placeholder_replace 函数中输入正确的 client 参数，client 当前只能是 web or ios")

    # 中文来一遍
    for i in en_list:
        new_i = ''
        for j in placeholder_replace_list:
            new_i = i.replace(f'{j}', f'{placeholder_replace_dict[j]}')
            i = new_i
        if client == 'web':
            if new_i.count("{{x}}") >= 2:
                for k in range(new_i.count("{{x}}")):
                    new_i = new_i.replace("{{x}}", random_letter[k], 1)
        if client == 'ios':
            if new_i.count("%d") >= 2:
                for k in range(new_i.count("%d")):
                    new_i = new_i.replace("%d", random_letter[k], 1)
        new_en_list.append(new_i)

    # 英语来一遍
    for i in cn_list:
        new_i = ''
        for j in placeholder_replace_list:
            new_i = i.replace(f'{j}', f'{placeholder_replace_dict[j]}')
            i = new_i
        if client == 'web':
            if new_i.count("{{x}}") >= 2:
                for k in range(new_i.count("{{x}}")):
                    new_i = new_i.replace("{{x}}", random_letter[k], 1)
        if client == 'ios':
            if new_i.count("%d") >= 2:
                for k in range(new_i.count("%d")):
                    new_i = new_i.replace("%d", random_letter[k], 1)
        new_cn_list.append(new_i)
    new_pd_result_data = {'en_英语': new_en_list, 'zh-CN_中文': new_cn_list}
    return new_pd_result_data
